package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.AnswertblBiz;
import com.njym.java.security.app.biz.QuestiontblBiz;
import com.njym.java.security.app.entity.Answertbl;
import com.njym.java.security.app.entity.Questiontbl;
import com.njym.java.security.app.entity.QuestiontblVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("questiontbl")
public class QuestiontblController extends BaseController<QuestiontblBiz,Questiontbl> {

    @Autowired
    private AnswertblBiz answertblBiz;

    /**
     * 通过问卷编号查询问卷对应的问题列表以及答案列表
     * @param limit
     * @param page
     * @param code
     * @return
     */
    @RequestMapping(value = "/getQuestionList",method = RequestMethod.GET)
    @ResponseBody
    public  Object list(
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(defaultValue = "1")int page,
            String code,Integer typetblId
    ){
        List<QuestiontblVo> question = baseBiz.queryList(code,typetblId);
        question.forEach(ques ->{
            Example example = new Example(Answertbl.class);
            example.createCriteria().andEqualTo("deleted",0).andEqualTo("questionId",ques.getId());
            List<Answertbl> answert = answertblBiz.selectByExample(example);
            ques.setAnswert(answert);
        });
        Map<String,Object> data = new HashMap<String,Object>(1);
        data.put("list",question);
        return new ObjectRestResponse<>().rel(true).data(question);
    }
}